package handler

import (
	"context"
	"github.com/google/uuid"
	"server/basic/global"
	"server/models"

	__ "server/proto"
)

type Server struct {
	__.UnimplementedBirdServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) SayHello(_ context.Context, in *__.Request) (*__.Response, error) {
	return &__.Response{}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) List(_ context.Context, in *__.ListRequest) (*__.ListResponse, error) {
	var find []models.Birds
	global.DB.Where("name = ?", in.Name).Find(&find)
	var finds []*__.List
	for _, birds := range find {
		lists := &__.List{
			Ji: int64(birds.Ji),
		}
		finds = append(finds, lists)
	}
	return &__.ListResponse{
		List: finds,
	}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Money(_ context.Context, in *__.MoneyRequest) (*__.MoneyResponse, error) {
	var m models.Birds
	global.DB.Find(&m)
	var money float64
	if m.Kg <= 1 {
		money = 8
	}
	if m.Kg > 1 {
		money = float64(m.Kg-1)*3 + 8
	}
	return &__.MoneyResponse{
		Money: int64(money),
	}, nil
}
func (s *Server) Get(_ context.Context, in *__.GetRequest) (*__.GetResponse, error) {
	str := uuid.New().String()
	add := models.Birds{
		Kg:      in.Kg,
		Address: in.Address,
		Ji:      int8(in.Ji),
	}
	global.DB.Create(&add)

	return &__.GetResponse{
		Number: str,
	}, nil
}
